package com.lisn.sgdouyin;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;

import com.lisn.sgdouyin.databinding.ActivitySGDouYinTestBinding;
import com.lisn.sgdouyin.camera.record.MediaRecorder;
import com.lisn.sgdouyin.camera.widget.DouyinView;
import com.lisn.sgdouyin.camera.widget.RecordButton;
import com.lisn.sgdouyin.video.VideoActivity;

public class SGDouYinTestActivity extends AppCompatActivity {

    private ActivitySGDouYinTestBinding binding;
    private int CAMERA_OK_CODE = 111;
    private DouyinView douyinView;

    public static void startActivity(Context context) {
        context.startActivity(new Intent(context, SGDouYinTestActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getPermission();
    }

    private void getPermission() {
        if (Build.VERSION.SDK_INT > 22) {
            if (ContextCompat.checkSelfPermission(SGDouYinTestActivity.this,
                    android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(SGDouYinTestActivity.this,
                        new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, CAMERA_OK_CODE);
                Log.i("TestOpenGLActivity", "requestPermissions");
            } else {
                Log.i("TestOpenGLActivity", "已经获取了权限");
                init();
            }
        } else {
            Log.i("TestOpenGLActivity", "这个说明系统版本在6.0之下，不需要动态获取权限。");
            init();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == CAMERA_OK_CODE && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            init();
        }
    }

    private void init() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_s_g_dou_yin_test);
        douyinView = binding.douyinView;
        binding.btnRecord.setOnRecordListener(new RecordButton.OnRecordListener() {
            @Override
            public void onRecordStart() {
                douyinView.startRecord();
            }

            @Override
            public void onRecordStop() {
                douyinView.stopRecord();
            }
        });

        binding.rgSpeed.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            /**
             * 选择录制模式
             * @param group
             * @param checkedId
             */
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb_extra_slow) { //极慢
                    douyinView.setSpeed(DouyinView.Speed.MODE_EXTRA_SLOW);
                } else if (checkedId == R.id.rb_slow) {
                    douyinView.setSpeed(DouyinView.Speed.MODE_SLOW);
                } else if (checkedId == R.id.rb_normal) {
                    douyinView.setSpeed(DouyinView.Speed.MODE_NORMAL);
                } else if (checkedId == R.id.rb_fast) {
                    douyinView.setSpeed(DouyinView.Speed.MODE_FAST);
                } else if (checkedId == R.id.rb_extra_fast) { //极快
                    douyinView.setSpeed(DouyinView.Speed.MODE_EXTRA_FAST);
                }
            }
        });

        ((CheckBox)findViewById(R.id.bigEye)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                douyinView.enableBigEye(isChecked);
            }
        });

        ((CheckBox)findViewById(R.id.stick)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                douyinView.enableStick(isChecked);
            }
        });

        ((CheckBox)findViewById(R.id.beauty)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                douyinView.enableBeauty(isChecked);
            }
        });

        ((CheckBox)findViewById(R.id.cb_switch)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                douyinView.switchCamera();
            }
        });

        /**
         * 录制完成
         */
        douyinView.setOnRecordFinishListener(new MediaRecorder.OnRecordFinishListener() {
            @Override
            public void onRecordFinish(final String path) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Intent intent = new Intent(SGDouYinTestActivity.this, VideoActivity.class);
                        intent.putExtra("path",path);
                        startActivity(intent);
                    }
                });
            }
        });

    }
}